#include "gameselect.h"
#include"game_snake.h"
#include"gameroom.h"
#include<QIcon>
#include<QPushButton>
#include<QSound>
#include<QPainter>
#include<QPixmap>
#include<QTextEdit>
#include<QFile>
#include<QTextStream>

GameSelect::GameSelect(QWidget *parent) : QWidget(parent)
{
    this->setWindowIcon(QIcon(":res/WidgetLogo.png"));
    this->setWindowTitle("难度选择");

    QPushButton* back=new QPushButton(this);
    GameRoom* gameroom=new GameRoom;


    back->setIcon(QIcon(":res/back.png"));
    //back->setStyleSheet("QPushButton{border:0px;}");
    connect(back,&QPushButton::clicked,[=](){
        this->close();
        //不可放在函数外
        game_snake* gameback=new game_snake;
        gameback->show();

        QSound::play(":res/clicked.wav");
    });
    QFont font("华文琥珀",24);
    QPushButton* simple=new QPushButton(this);
    //simple->setStyleSheet("QPushButton{border:0px;}");
    simple->setFont(font);
    simple->move(420,200);
    simple->setText("简单模式");
    connect(simple,&QPushButton::clicked,[=](){
        this->close();
        gameroom->setGeometry(this->geometry());
        gameroom->show();

        gameroom->setTimeout(300);//设置各个模式的速度
        QSound::play(":res/clicked.wav");
    });


    QPushButton* normal=new QPushButton(this);
    normal->setFont(font);
    normal->move(420,300);
    normal->setText("进阶模式");
    connect(normal,&QPushButton::clicked,[=](){
        this->close();
        gameroom->setGeometry(this->geometry());
        gameroom->show();

        gameroom->setTimeout(200);
        QSound::play(":res/clicked.wav");
    });

    QPushButton* difficult=new QPushButton(this);
    difficult->setFont(font);
    difficult->move(420,400);
    difficult->setText("地域模式");
    connect(difficult,&QPushButton::clicked,[=](){
        this->close();
        gameroom->setGeometry(this->geometry());
        gameroom->show();

        gameroom->setTimeout(100);
        QSound::play(":res/clicked.wav");
    });

    QPushButton* history=new QPushButton(this);
    history->setFont(font);
    history->move(420,500);
    history->setText("历史战绩");

    connect(history,&QPushButton::clicked,[=]{
        QWidget* his=new QWidget;
        his->setWindowTitle("历史战绩");
        his->setWindowIcon(QIcon(":res/WidgetLogo.png"));
        his->setFixedSize(500,300);


        QTextEdit* edit=new QTextEdit(his);
        edit->setFixedSize(his->width(),his->height());
        QFile file("F:/Qt/snake/history.txt");
        file.open(QIODevice::ReadOnly);//打开文件

        //获取上次游戏结束蛇的长度
        QTextStream in(&file);//创建**文件对象
        int data=in.readLine().toInt();//读取一行数据
        edit->append(QString("得分为："));
        edit->append(QString::number(data));//将读取的数据写到文本编辑器（edit）上去
        edit->setFont(font);
        his->show();
    });

}

void GameSelect::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pic(":res/game_select.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pic);

}
